//
//  ZLDateExtension.swift
//  ZLExtension
//
//  Created by 赵振磊 on 2023/1/8.
//

import UIKit

public extension Date {
    /// 当前日期的年
    func zl_year() -> Int {
        let calendar = NSCalendar.current
        let com = calendar.dateComponents([.year,.month,.day], from: self)
        return com.year!
    }
    /// 当前日期的月
    func zl_month() -> Int {
        let calendar = NSCalendar.current
        let com = calendar.dateComponents([.year,.month,.day], from: self)
        return com.month!
    }
    /// 当前日期的日
    func zl_day() -> Int {
        let calendar = NSCalendar.current
        let com = calendar.dateComponents([.year,.month,.day], from: self)
        return com.day!
    }
    /// 当前日期是星期几
    func zl_weekDay() -> Int {
        let interval = Int(self.timeIntervalSince1970)
        let days = Int(interval / 86400) // 24*60*60
        let weekday = ((days + 4) % 7 + 7) % 7
        return weekday == 0 ? 7 : weekday
    }
    /// 当前日期是当前月的第几天
    func zl_dayInMonth() -> Int {
        let calendar = Calendar(identifier:Calendar.Identifier.gregorian)
        let range = (calendar as NSCalendar?)?.range(of: NSCalendar.Unit.day, in: NSCalendar.Unit.month, for: self)
        return (range?.length)!
    }
    // MARK: - 日期的一些比较
    /// 是否是今天
    func zl_isToday() -> Bool {
        let calendar = NSCalendar.current
        let com = calendar.dateComponents([.year,.month,.day], from: self)
        let comNow = calendar.dateComponents([.year,.month,.day], from: Date())
        return com.year == comNow.year && com.month == comNow.month && com.day == comNow.day
    }
    /// 是否是这个月
    func zl_isCurrentMonth() -> Bool {
        let calendar = NSCalendar.current
        let com = calendar.dateComponents([.year,.month,.day], from: self)
        let comNow = calendar.dateComponents([.year,.month,.day], from: Date())
        return com.year == comNow.year && com.month == comNow.month
    }
    /// 是否是今年
    func zl_isCurrentYear() -> Bool {
        let calendar = NSCalendar.current
        let com = calendar.dateComponents([.year,.month,.day], from: self)
        let comNow = calendar.dateComponents([.year,.month,.day], from: Date())
        return com.year == comNow.year
    }
    /// date转日期字符串
    func zl_toString(dateFormat: String = "YYYY-MM-dd") -> String {
        let timeZone = NSTimeZone.local
        let formatter = DateFormatter()
        formatter.timeZone = timeZone
        formatter.dateFormat = dateFormat
        let date = formatter.string(from: self)
        return date
    }
    /// 计算天数差
    func zl_differDays(date: Date) -> Int {
        let calendar = Calendar.current
        let components = calendar.dateComponents([.day], from: self, to: date)
        return components.day ?? 0
    }
    /// 计算月、天差
    func zl_differMonthsDays(date: Date) -> (months: Int, days: Int) {
        let calendar = Calendar.current
        let components = calendar.dateComponents([.month, .day], from: self, to: date)
        
        if let months = calendar.dateComponents([.month], from: self, to: date).month,
           let intermediateDate = calendar.date(byAdding: .month, value: months, to: self) {
            
            let days = calendar.dateComponents([.day], from: intermediateDate, to: date).day ?? 0
            return (months, days)
        }
        
        return (0, 0)
    }
    /// 比较时间先后
    func zl_compare(date: Date) -> ComparisonResult {
        let dateFormatter:DateFormatter = DateFormatter()
        dateFormatter.dateFormat = "yyyy-MM-dd"
        let oneDayStr:String = dateFormatter.string(from: self)
        let anotherDayStr:String = dateFormatter.string(from: date)
        let dateA = dateFormatter.date(from: oneDayStr)
        let dateB = dateFormatter.date(from: anotherDayStr)
        let result:ComparisonResult = (dateA?.compare(dateB!))!
        return result
    }
    /// 获取时间日的偏移
    func zl_offsetDay(day: Int) -> Date {
        let date = Calendar.current.date(byAdding: .day, value: day, to: self)!
        return date
    }
    /// 获取最小时间
    func zl_minDay() -> Date {
        let dateComponents = DateComponents(year: self.zl_year(), month: self.zl_month(), day: self.zl_day(), hour: 0, minute: 0, second: 0)
        let date = Calendar.current.date(from: dateComponents)!
        return date
    }
    /// 获取最大时间
    func zl_maxDay() -> Date {
        let dateComponents = DateComponents(year: self.zl_year(), month: self.zl_month(), day: self.zl_day(), hour: 23, minute: 59, second: 59)
        let date = Calendar.current.date(from: dateComponents)!
        return date
    }
    /// 获取时间戳
    func zl_timestamp() -> Int {
        return Int(self.timeIntervalSince1970)
    }
    /// 时间差
    func zl_diffString(date: Date) -> String {
        let calendar = Calendar.current
        let secondsDiff = Int(self.timeIntervalSince(date))
        if secondsDiff < 60 {
            return "1分内"
        }
        let components = calendar.dateComponents([.year, .month, .day, .hour, .minute], from: date, to: self)
        var parts: [String] = []
        if let year = components.year, year > 0 {
            parts.append("\(year)年")
        }
        if let month = components.month, month > 0 {
            parts.append("\(month)月")
        }
        if let day = components.day, day > 0 {
            parts.append("\(day)天")
        }
        if let hour = components.hour, hour > 0 {
            parts.append("\(hour)时")
        }
        if let minute = components.minute, minute > 0 {
            parts.append("\(minute)分")
        }
        return parts.joined()
    }
}

public extension Date {
    
    // MARK: 快速获取
    /// 当前年
    static func zl_currentYear() -> Int {
        let calendar = NSCalendar.current
        let com = calendar.dateComponents([.year,.month,.day], from: Date())
        return com.year!
    }
    /// 当前月
    static func zl_currentMonth() -> Int {
        let calendar = NSCalendar.current
        let com = calendar.dateComponents([.year,.month,.day], from: Date())
        return com.month!
    }
    /// 当前日
    static func zl_currentDay() -> Int {
        let calendar = NSCalendar.current
        let com = calendar.dateComponents([.year,.month,.day], from: Date())
        return com.day!
    }
    /// 今天是星期几
    static func zl_currentWeekDay()->Int{
        let interval = Int(Date().timeIntervalSince1970)
        let days = Int(interval / 86400) // 24*60*60
        let weekday = ((days + 4) % 7 + 7) % 7
        return weekday == 0 ? 7 : weekday
    }
    /// 当前月有多少天
    static func zl_daysOfCurrentMonth() -> Int {
        let calendar = Calendar(identifier:Calendar.Identifier.gregorian)
        let range = (calendar as NSCalendar?)?.range(of: NSCalendar.Unit.day, in: NSCalendar.Unit.month, for: Date())
        return (range?.length)!
    }
    /// 当前月第一天是星期几
    static func zl_firstDayInCurrentMonth() -> Int {
        // 星期和数字一一对应 星期日:7
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "yyyy-MM"
        let date = dateFormatter.date(from: String(Date().zl_year()) + "-" + String(Date().zl_month()))
        let calender = Calendar(identifier:Calendar.Identifier.gregorian)
        let comps = (calender as NSCalendar?)?.components(NSCalendar.Unit.weekday, from: date!)
        var week = comps?.weekday
        if week == 1 {
            week = 8
        }
        return week! - 1
    }
    ///获取系统的当前时间戳
    static func zl_timeStamp() -> Int {
        let date = Date()
        let timeInterval: Int = Int(date.timeIntervalSince1970)
        return timeInterval
    }
}
